﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Spotshub.Plugin.Payments.GlobalPayments.Services
{
    public static class NameValueCollectionExtensions
    {
        public static void AddNonEmptyValue(this NameValueCollection nv, string name, string val)
        {
            if (!String.IsNullOrWhiteSpace(val))
            {
                nv.Add(name, val);
            }
        }

        public static string ToQueryString(this NameValueCollection nv)
        {
            var array = (from key in nv.AllKeys
                         from value in nv.GetValues(key)
                         select string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value)))
                .ToArray();
            return "?" + string.Join("&", array);
        }
    }
}
